package com.ruoyi.car.controller;

import java.util.List;

import com.ruoyi.car.domain.TmdCarRecord;
import com.ruoyi.car.domain.TmdCarUse;
import com.ruoyi.car.service.ITmdCarRecordService;
import com.ruoyi.car.service.ITmdCarUseService;
import com.ruoyi.car.vo.ListAndUserVO;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import net.sf.ehcache.store.disk.ods.AATreeSet;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.car.domain.TmdCarList;
import com.ruoyi.car.service.ITmdCarListService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆信息Controller
 *
 * @author ruoyi
 * @date 2024-04-19
 */
@Controller
@RequestMapping("/car/manage")
public class TmdCarListController extends BaseController {
    private String prefix = "car/manage";

    @Autowired
    private ITmdCarListService tmdCarListService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITmdCarUseService tmdCarUseService;


    @RequiresPermissions("car:manage:view")
    @GetMapping()
    public String manage() {
        return prefix + "/manage";
    }

    /**
     * 查询车辆信息列表
     */
    @RequiresPermissions("car:manage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TmdCarList tmdCarList) {
        startPage();
        List<TmdCarList> list = tmdCarListService.selectTmdCarListList(tmdCarList);
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @RequiresPermissions("car:manage:export")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TmdCarList tmdCarList) {
        List<TmdCarList> list = tmdCarListService.selectTmdCarListList(tmdCarList);
        ExcelUtil<TmdCarList> util = new ExcelUtil<TmdCarList>(TmdCarList.class);
        return util.exportExcel(list, "车辆信息数据");
    }

    /**
     * 新增车辆信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆信息
     */
    @RequiresPermissions("car:manage:add")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TmdCarList tmdCarList) {
        return toAjax(tmdCarListService.insertTmdCarList(tmdCarList));
    }

    /**
     * 修改车辆信息
     */
    @RequiresPermissions("car:manage:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ListAndUserVO tmdCarList = tmdCarListService.selectTmdCarListById(id);
        mmap.put("tmdCarList", tmdCarList);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆信息
     */
    @RequiresPermissions("car:manage:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdCarList tmdCarList) {
        return toAjax(tmdCarListService.updateTmdCarList(tmdCarList));
    }

    /**
     * 删除车辆信息
     */
    @RequiresPermissions("car:manage:remove")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tmdCarListService.deleteTmdCarListByIds(ids));
    }

    /**
     * 查看车辆信息
     */
    @RequiresPermissions("car:manage:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        ListAndUserVO tmdCarList = tmdCarListService.selectTmdCarListById(id);
        mmap.put("tmdCarList", tmdCarList);
        return prefix + "/detail";
    }


    /**
     * 获取所有用户
     *
     * @return
     */
    @GetMapping("/getUserList")
    @ResponseBody
    public AjaxResult getUserInfo() {
        List<SysUser> users = sysUserService.getUserInfo();
        if (users != null) {
            return AjaxResult.success("获取用户信息成功", users);
        } else {
            return AjaxResult.error("获取用户信息失败");
        }
    }

    /**
     * 根据用户id获取手机号
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserPhoneNumber")
    @ResponseBody
    public AjaxResult getUserPhoneNumber(@RequestParam Long userId) {
        SysUser userInfo = sysUserService.getUserByUserId(userId);
        if (userInfo != null) {
            return AjaxResult.success("获取用户信息成功", userInfo);
        } else {
            return AjaxResult.error("获取用户信息失败");
        }
    }

    /**
     * 根据车辆id获取车辆信息
     *
     * @param carId
     * @return
     */
    @GetMapping("/{carId}")
    @ResponseBody
    public AjaxResult getCarInfo(@PathVariable int carId) {
        TmdCarList carInfo = tmdCarListService.getCarInfo(carId);
        if (carInfo != null) {
            return AjaxResult.success("获取车辆信息成功", carInfo);
        } else {
            return AjaxResult.error("获取车辆信息失败");
        }
    }


    /**
     * 获取所有部门
     *
     * @return
     */
    @GetMapping("/getDepartments")
    @ResponseBody
    public AjaxResult getDeptInfo() {
        List<SysDept> depts = sysDeptService.getDeptInfo();
        if (depts != null) {
            return AjaxResult.success("获取部门信息成功", depts);
        } else {
            return AjaxResult.error("获取部门信息失败");
        }
    }

    /**
     * 提交用车申请
     */
    @PostMapping("/apply")
    @ResponseBody
    public AjaxResult applyForVehicle(TmdCarUse carUse) {
        if (tmdCarUseService.insertTmdCarUse(carUse) > 0) {
            return AjaxResult.success("成功");
        } else {
            return AjaxResult.error("失败");
        }
    }


    @GetMapping("/carStatusList")
    @ResponseBody
    public AjaxResult getCarStatusList() {
        try {
            // 调用服务层方法获取所有车辆的状态列表
            List<Integer> carStatusList = tmdCarListService.getAllCarStatus();
            // 返回成功响应，将车辆状态列表作为数据返回
            return AjaxResult.success("获取车辆状态列表成功", carStatusList);
        } catch (Exception e) {
            // 处理异常
            e.printStackTrace(); // 或者记录日志
            // 返回失败响应
            return AjaxResult.error("获取车辆状态列表失败");
        }
    }

    @PostMapping("updateCarStatus")
    @ResponseBody
    public AjaxResult updateCarStatus(@RequestParam("carId") Long carId) {
        try {
            if (tmdCarListService.updateCarStatus(carId) > 0) {
                return AjaxResult.success("成功更新车辆状态");
            } else {
                return AjaxResult.error("更新车辆状态失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("更新车辆状态失败");
        }
    }

//    @RequiresPermissions("car:manage:add")
//    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(TmdCarList tmdCarList) {
//        return toAjax(tmdCarListService.insertTmdCarList(tmdCarList));
//    }

}
